﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic.Web;
using LotteryVote.Logic;
using LotteryVote.Logic.Exceptions;
using LotteryVote.Utility;
using LotteryVote.Entity;

namespace LotteryVote
{
    public partial class UpdateUserPassword : AuthorizePage
    {
        protected User UpdateUser;
        UserFacade userFacade = new UserFacade();
        protected void Page_Load(object sender, EventArgs e)
        {
            InitParams();
        }

        void InitParams()
        {
            int userId;
            if (string.IsNullOrEmpty(Request["UserId"]) || !int.TryParse(Request["UserId"], out userId))
            {
                Response.Redirect("~/");
                return;
            }
            UpdateUser = userFacade.GetUser(userId);
            if (UpdateUser == null || (UpdateUser.UserId != CurrentUser.UserId && UpdateUser.ParentId != CurrentUser.UserId))
                throw new PromptException("用户{0}没有修改UserId：{1}的权限!", CurrentUser.UserId, userId);
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                if (!EncryptHelper.Equal(txt_myPassword.Text, CurrentUser.UserInfo.Password))
                {
                    ShowMessage("Error", "请输入正确的密码!");
                    return;
                }
                if (txt_password.Text != txt_confirPwd.Text)
                {
                    ShowMessage("Error", "两次输入密码不一样!");
                    return;
                }
                userFacade.UpdatePassword(UpdateUser, txt_password.Text);
                ShowMessage("Result", GetGlobalResourceObject("Resource", "Success").ToString());
            });
        }
    }
}